//
//  ReflectionView.m
//  Objective-C
//
//  Created by S_Line on 2018/6/29.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "ReflectionView.h"

@interface ReflectionView()

@property (nonatomic, weak) CAReplicatorLayer *reflectionLayer;

@end

@implementation ReflectionView

- (CAReplicatorLayer *)reflectionLayer {
    return (CAReplicatorLayer *)self.layer;
}

+ (Class)layerClass {
    return [CAReplicatorLayer class];
}

- (void)setUp {
    // configure replicator
    self.reflectionLayer.instanceCount = 2;
    
    // move reflection instance below original and flip vertically
    CATransform3D transform = CATransform3DIdentity;
    CGFloat verticalOffset = self.bounds.size.height + 2;
    transform = CATransform3DTranslate(transform, 0, verticalOffset, 0);
    transform = CATransform3DScale(transform, 0, -1, 0);
    self.reflectionLayer.instanceTransform = transform;
    
    // reduce alpha of reflection layer
    self.reflectionLayer.instanceAlphaOffset = -0.5;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        [self setUp];
    }
    
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUp];
}

- (void)addSubview:(UIView *)view {
    [super addSubview:view];
    [self setUp];
}

@end
